import unittest

from psychsim.pwl import *
from psychsim.action import Action,ActionSet
from psychsim.world import World,stateKey
from psychsim.agent import Agent
from psychsim.reward import *



class NestedBeliefsTestCase(unittest.TestCase):

    def setUp(self):
        #setup world, agent, and basics
        self.maxRounds=5
        self.world = World()
        self.son = Agent('Son')
        self.world.addAgent(self.son)
        self.father = Agent('Father')
        self.world.addAgent(self.father)

        self.world.setOrder(['Son'])        

    def addStates(self):
        #define states
        self.world.defineState(None,'round',int)
        self.world.setState(None,'round',0)
        
        self.world.defineState('Son','energy',int)
        self.world.setState('Son','energy',10)
                    
    def addTermination(self):
        #terminate if maxRounds reached
        self.world.addTermination(makeTree({'if': thresholdRow(stateKey(None,'round'),self.maxRounds),
                                   True: True, False: False}))

    def addActions(self):
        #add actions and appropriate dynamics
        action =  self.son.addAction({'verb': 'do nothing'})
        tree = makeTree(incrementMatrix(stateKey(None,'round'),1))
        self.world.setDynamics('round',action,tree) 
        
        action =  self.son.addAction({'verb': 'do something'})
        tree = makeTree(incrementMatrix(stateKey('Son','energy'),-1))
        self.world.setDynamics(stateKey('Son','energy'),action,tree) 
        tree = makeTree(incrementMatrix(stateKey(None,'round'),1))
        self.world.setDynamics('round',action,tree) 

    def setModelAndRewards(self):
        #set models, rewards, and beliefs
        self.son.addModel('Lazy',R={},rationality=1.,horizon=0) # setting the horizon to 0 prevents the infinite loop
        
        self.father.addModel('Judgemental',rationality=1.)
        
        self.son.setReward(maximizeFeature(stateKey('Son','energy')), 1., True)
        self.son.setReward(maximizeFeature(stateKey('Son','energy')), 3., 'Lazy')

        beliefs = {'Judgemental':1.}
        self.world.setMentalModel('Son', 'Father', beliefs, True)
        beliefs = {'Lazy':1.}
        
        self.world.setMentalModel('Father', 'Son', beliefs, 'Judgemental')
        
        self.world.setModel('Son', True)
        self.world.setModel('Father', True)

    def testSetup(self):
        self.addStates()
        self.addTermination()
        self.addActions()
        self.setModelAndRewards()

    def testRun(self):
        self.testSetup()
        self.world.printModels()
        for t in range(self.maxRounds + 1):
            self.world.explain(self.world.step(),level=1)
            self.world.state.select()
            self.world.printState()
            self.world.printModels()
            if self.world.terminated():
                break 

if __name__ == "__main__":
    suite = unittest.TestSuite()
    for method in dir(NestedBeliefsTestCase):
       if method.startswith("test"):
          suite.addTest(NestedBeliefsTestCase(method))
    unittest.TextTestRunner().run(suite)
            